capture log close
clear all
set maxvar 30000
set more off
pause off
# delimit;

local crime "";
local output "";

/* 
Table B4
Covariate balance test
*/


/* ************************* */
/* **** GET THE P-VALUE **** */
/* ************************* */

use "`crime'/prwora_crime_covbal.dta" ;

keep covbal2 ; 

keep if _n==1 ; 

gen variables = "p_value" ;

order variables covbal2 ;

rename covbal2 control_mean ; 

tempfile p_value ;
save `p_value' ;


/* *********************** */
/* **** PREPARE TABLE **** */
/* *********************** */

use "`crime'/prwora_crime_summstats_exactage.dta", clear ;

keep if label == "prec_rd_mean" ;

keep an_male an_firstage an_fam_singmom an_fam_nopar an_diag1_mental
	 an_diag1_nerv an_diag1_sense an_diag1_infec an_diag1_endo
	 an_diag1_none an_diag1_cong an_diag1_musc an_diag1_resp an_diag1_blood
	 an_diag1_neo an_earnhhpre9095;

xpose, clear varname ;

rename v1 control_mean ;
rename _varname variables ;
order variables control_mean ;

replace variables = subinstr(variables,"an_", "", .) ;

tempfile mean ;
save `mean' ;


clear;

use "`crime'/prworaexact_bw264_crime_poly1nocov_rd_all.dta" ;

keep if variables == "n_male" | variables == "n_firstage" | 
		variables == "n_fam_singmom" | variables == "n_fam_nopar" | 
		variables == "n_diag1_mental" | variables == "n_diag1_nerv" | 
		variables == "n_diag1_sense" | variables == "n_diag1_infec" | 
		variables == "n_diag1_endo" | variables == "n_diag1_none" | 
		variables == "n_diag1_cong" | variables == "n_diag1_musc" | 
		variables == "n_diag1_resp" | variables == "n_diag1_blood" | 
		variables == "n_diag1_neo" | variables == "n_earnhhpre9095" ;
		
keep variables n_post sen_post N ;

rename n_post pt_est ;
rename sen_post se ;

replace variables = subinstr(variables,"n_", "", .) ;

merge 1:1 variables using `mean' ;
drop _merge ;


replace variables = subinstr(variables,"diag1_", "", .) ;
replace variables = subinstr(variables,"fam_", "", .) ;


replace variables = "Blood" if variables == "blood" ;
replace variables = "Congenital" if variables == "cong" ;
replace variables = "Endocrine" if variables == "endo" ;
replace variables = "Infectious" if variables == "infec" ;
replace variables = "Mental" if variables == "mental" ;
replace variables = "Musculoskeletal" if variables == "musc" ;
replace variables = "Neoplasm" if variables == "neo" ;
replace variables = "Nervous" if variables == "nerv" ;
replace variables = "Missing" if variables == "none" ;
replace variables = "Respiratory" if variables == "resp" ;
replace variables = "Sensory" if variables == "sense" ;
replace variables = "HH earnings (1990-1995)" if variables == "earnhhpre9095" ;
replace variables = "No parent" if variables == "nopar" ;
replace variables = "Single parent" if variables == "singmom" ;
replace variables = "Age at entry" if variables == "firstage" ;
replace variables = "Male" if variables == "male" ;


gen order = 1 if variables == "Male" ;
replace order = 2 if variables == "Age at entry" ;
replace order = 3 if variables == "Single parent" ;
replace order = 4 if variables == "No parent" ;
replace order = 5 if variables == "Mental" ;
replace order = 6 if variables == "Nervous" ;
replace order = 7 if variables == "Sensory" ;
replace order = 8 if variables == "Infectious" ;
replace order = 9 if variables == "Endocrine" ;
replace order = 10 if variables == "Congenital" ;
replace order = 11 if variables == "Musculoskeletal" ;
replace order = 12 if variables == "Respiratory" ;
replace order = 13 if variables == "Blood" ;
replace order = 14 if variables == "Neoplasm" ;
replace order = 15 if variables == "Missing" ;
replace order = 16 if variables == "HH earnings (1990-1995)" ;


sort order ;
	
drop order ;

order variables pt_est se control_mean ;

tostring se, replace ;
replace se = "(" + se + ")" ;

append using `p_value' ;


export excel using "`output'/tabB4.xls", firstrow(variables) ;

